package com.youi.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.youi.shop.commons.exceptions.GlobalException;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.pojo.entity.Cart;
import com.youi.shop.pojo.entity.Product;
import com.youi.shop.pojo.qo.cart.CartInsertQO;
import com.youi.shop.pojo.qo.cart.CartUpdateQO;
import com.youi.shop.pojo.vo.cart.CartVO;
import com.youi.shop.pojo.vo.product.ProductVO;
import com.youi.shop.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车业务接口")
public class CartController {

    @Resource
    CartService cartService;

    /**
     * 将单个商品添加到购物车
     *
     * @param cartInsertQO
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/addCart")
    public RespEntity<List<CartVO>> addCart(@Validated @RequestBody CartInsertQO cartInsertQO) {

        return cartService.addOne(cartInsertQO);
    }

    /**
     * 根据会员id查询所有购物车信息
     *
     * @param memberId
     * @return
     */
    @ApiOperation("根据会员ID查询所有购物车信息")
    @GetMapping("/cartList/{memberId}")
    public RespEntity<List<CartVO>> selectByMemberId(@PathVariable Long memberId) {

        return cartService.selectByMemberId(memberId);
    }


    /**
     * 修改购物车1.0
     *
     * @param cartUpdateQO
     * @return
     */
    @ApiOperation("修改购物车")
    @PostMapping("/modifyCart")
    public RespEntity<Object> modify(@RequestBody @Validated CartUpdateQO cartUpdateQO) {
        //根据id查询购物车中是否有该id的数据
        Cart cart = cartService.selectByCartId(cartUpdateQO.getCartId());
        //如果有就修改数量和计算金额
        if (cart != null) {
            //更新购物车数量
            //更新购物车金额

            UpdateWrapper<Cart> wrapper = new UpdateWrapper<>();
            wrapper.eq("cart_id", cartUpdateQO.getCartId())
                    .set("quantity", cartUpdateQO.getQuantity())
                    .set("total_amount", cart.getProduct().getProductPrice().multiply(new BigDecimal(cartUpdateQO.getQuantity())));
            boolean flag = cartService.update(cart, wrapper);
            if (flag) {

                cart.setQuantity(cartUpdateQO.getQuantity());
                cart.setTotalAmount(cart.getProduct().getProductPrice().multiply(new BigDecimal(cartUpdateQO.getQuantity())));
                //返回成功给客户端
                CartVO cartVO = new CartVO();
                Product product = cart.getProduct();
                ProductVO productVO = new ProductVO();
                BeanUtils.copyProperties(product, productVO);
                cartVO.setProductVO(productVO);
                BeanUtils.copyProperties(cart, cartVO);

                return RespEntity.success(cartVO);
            } else {
                throw new GlobalException(StatusCode.CART_UPDATE_ERROR);

            }

        }

        throw new GlobalException(StatusCode.CART_NOTFOUND_ERROR);


    }

    /**
     * 删除购物车
     *
     * @param cartId
     * @return
     */
    @ApiOperation("删除购物车")
    @PostMapping("/deleteCart")
    public RespEntity<Object> deleteById(@RequestParam Long cartId) {

        boolean flag = cartService.removeById(cartId);
        if (flag) {
            return RespEntity.success(null);

        } else {
            throw new GlobalException(StatusCode.CART_DELETE_ERROR);
        }
    }


    /**
     * 清空购物车
     *
     * @param memberId
     * @return
     */
    @ApiOperation("清空购物车")
    @PostMapping("/clearCart")
    public RespEntity<Object> clearCartByMemberId(@RequestParam Long memberId) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        boolean flag = cartService.remove(wrapper);
        if (flag) {
            //清空成功
            return RespEntity.success(null);
        } else {
            //清空失败
            throw new GlobalException(StatusCode.CART_CLEAR_ERROR);
        }
    }

}
